package com.ruoyi.api.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.DzHyuser;
import com.ruoyi.system.service.IDzHyuserService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DzHuiyi;
import com.ruoyi.system.service.IDzHuiyiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议管理Controller
 * 
 * @author ruoyi
 * @date 2023-11-12
 */
@RestController
@RequestMapping("/api/huiyi")
public class DzHuiyiApiController extends BaseController
{
    @Autowired
    private IDzHuiyiService dzHuiyiService;
    @Autowired
    private IDzHyuserService dzHuiyiUserService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询会议管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DzHuiyi dzHuiyi)
    {
        startPage();
        List<DzHuiyi> list = dzHuiyiService.selectDzHuiyiListWeb(dzHuiyi);
        return getDataTable(list);
    }

    /**
     * 导出会议管理列表
     */
    @Log(title = "会议管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DzHuiyi dzHuiyi)
    {
        List<DzHuiyi> list = dzHuiyiService.selectDzHuiyiListWeb(dzHuiyi);
        ExcelUtil<DzHuiyi> util = new ExcelUtil<DzHuiyi>(DzHuiyi.class);
        util.exportExcel(response, list, "会议管理数据");
    }

    /**
     * 获取会议管理详细信息
     */
    @GetMapping(value = "/{hyId}")
    public AjaxResult getInfo(@PathVariable("hyId") Long hyId)
    {
        return AjaxResult.success(dzHuiyiService.selectDzHuiyiByHyId(hyId));
    }

    /**
     * 新增会议管理
     */
    @Log(title = "会议管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DzHuiyi dzHuiyi)
    {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        dzHuiyi.setYqcode(uuid.substring(0,8));
        if(dzHuiyi.getUserName()!=null){
          SysUser user =   sysUserService.selectUserByUserName(dzHuiyi.getUserName());
          if(user!=null){
              dzHuiyi.setUserId(user.getUserId());
              dzHuiyi.setCreateTime(new Date());
              dzHuiyi.setState(0);
              dzHuiyiService.insertDzHuiyi(dzHuiyi);

              return  new AjaxResult(200,"操作成功",dzHuiyi);
          }else{
              return error("用户未在会议系统中注册");
          }

        }else{
            return error("用户未在会议系统中注册");
        }
    }


    @PostMapping("/jion")
    public AjaxResult jion(@RequestBody DzHuiyi dzHuiyi)
    {
        List<DzHuiyi> hylist = dzHuiyiService.selectDzHuiyiListWeb(dzHuiyi);
        if(hylist!=null&&hylist.size()>0){
            DzHuiyi hy = hylist.get(0);
            if(hy.getState()==0){
                DzHyuser hyuser = new DzHyuser();
                hyuser.setHyId(hy.getHyId());
                hyuser.setUserId(dzHuiyi.getUserId());
                List<DzHyuser> hyuserlist = dzHuiyiUserService.selectDzHyuserList(hyuser);
                if(hyuserlist!=null&&hyuserlist.size()>0){
                    DzHyuser temp = hyuserlist.get(0);
                    temp.setState(0);

                    return toAjax(dzHuiyiUserService.updateDzHyuser(temp));
                }else{
                    DzHyuser temp = new DzHyuser();
                    temp.setState(0);
                    temp.setHyId(hy.getHyId());
                    temp.setUserId(dzHuiyi.getUserId());
                    temp.setJoinTime(new Date());
                    temp.setUserName(dzHuiyi.getUserName());

                    return toAjax(dzHuiyiUserService.insertDzHyuser(temp));
                }

            }else{
                return error("会议已结束");
            }

        }else{
            return error("邀请码未查询到相应的会议");
        }
    }


    @PostMapping("/leave")
    public AjaxResult leave(@RequestBody DzHuiyi dzHuiyi)
    {
        List<DzHuiyi> hylist = dzHuiyiService.selectDzHuiyiListWeb(dzHuiyi);
        if(hylist!=null&&hylist.size()>0){
            DzHuiyi hy = hylist.get(0);
            if(hy.getState()==0){
                DzHyuser hyuser = new DzHyuser();
                hyuser.setHyId(hy.getHyId());
                hyuser.setUserId(dzHuiyi.getUserId());
                List<DzHyuser> hyuserlist = dzHuiyiUserService.selectDzHyuserList(hyuser);
                if(hyuserlist!=null&&hyuserlist.size()>0){
                    DzHyuser temp = hyuserlist.get(0);
                    temp.setState(1);
                    temp.setLeaveTime(new Date());
                    return toAjax(dzHuiyiUserService.updateDzHyuser(temp));
                }else{
                    DzHyuser temp = new DzHyuser();
                    temp.setState(1);
                    temp.setHyId(hy.getHyId());
                    temp.setUserId(dzHuiyi.getUserId());
                    temp.setLeaveTime(new Date());
                    temp.setUserName(dzHuiyi.getUserName());
                    return toAjax(dzHuiyiUserService.insertDzHyuser(temp));
                }

            }else{
                return error("会议已结束");
            }

        }else{
            return error("邀请码未查询到相应的会议");
        }
    }

    @PostMapping("/end")
    public AjaxResult end(@RequestBody DzHuiyi dzHuiyi)
    {
        List<DzHuiyi> hylist = dzHuiyiService.selectDzHuiyiListWeb(dzHuiyi);
        if(hylist!=null&&hylist.size()>0){
            DzHuiyi hy = hylist.get(0);
            if(hy.getState()==0){
                hy.setState(1);
                return toAjax(dzHuiyiService.updateDzHuiyi(hy));
//                DzHyuser hyuser = new DzHyuser();
//                hyuser.setHyId(hy.getHyId());
//                hyuser.setUserId(dzHuiyi.getUserId());
//                List<DzHyuser> hyuserlist = dzHuiyiUserService.selectDzHyuserList(hyuser);
//                if(hyuserlist!=null&&hyuserlist.size()>0){
//                    DzHyuser temp = hyuserlist.get(0);
//                    temp.setState(1);
//                    temp.setLeaveTime(new Date());
//                    return toAjax(dzHuiyiUserService.updateDzHyuser(temp));
//                }else{
//                    DzHyuser temp = new DzHyuser();
//                    temp.setState(1);
//                    temp.setHyId(hy.getHyId());
//                    temp.setUserId(dzHuiyi.getUserId());
//                    temp.setLeaveTime(new Date());
//                    temp.setUserName(dzHuiyi.getUserName());
//                    return toAjax(dzHuiyiUserService.insertDzHyuser(temp));
//                }

            }else{
                return error("会议已结束");
            }

        }else{
            return error("邀请码未查询到相应的会议");
        }
    }

    /**
     * 修改会议管理
     */
    @Log(title = "会议管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DzHuiyi dzHuiyi)
    {
        return toAjax(dzHuiyiService.updateDzHuiyi(dzHuiyi));
    }

    /**
     * 删除会议管理
     */
    @Log(title = "会议管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hyIds}")
    public AjaxResult remove(@PathVariable Long[] hyIds)
    {
        return toAjax(dzHuiyiService.deleteDzHuiyiByHyIds(hyIds));
    }
}
